<?php
class User_Model_Action
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new User_Model_DbTable_Action();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function create($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ura_id 
	*@return boolean
	*/
	public  function update($row,$ura_id){
	
		$where = $this->_table->getAdapter()->quoteInto('ura_id = ?', $ura_id);
 
		return $this->_table->update($row, $where);
		
	}
		
	/**
	*Delete One Row
	*@param int $ura_id 
	*@return boolean
	*/
	public function delete($ura_id){
	
		$where = $this->_table->getAdapter()->quoteInto('ura_id = ?', $ura_id);		 
 
		return $this->_table->delete($where);
		
	}	

    public function getByCondition($condition,$type='*',$orderby="ura_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        if(isset($condition["ura_controller"]) && $condition["ura_controller"]!=''){
            $select->where(" ura_controller =?",$condition["ura_controller"]);
        }
        if(isset($condition["ura_module"]) && $condition["ura_module"]!=''){
            $select->where(" ura_module =?",$condition["ura_module"]);
        }
        if(isset($condition["ura_action"]) && $condition["ura_action"]!=''){
            $select->where(" ura_action =?",$condition["ura_action"]);
        }		
		if(isset($condition["ura_title"]) && $condition["ura_title"]!=''){
            $select->where(" ura_title like ?","%".$condition["ura_title"]."%");
        }		
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();

		    return $this->_table->getAdapter()->fetchAll($sql);
        }
		
	}	
	
	
	public function getSelect($type='*',$orderby="ura_id"){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);	
		$select->group('ura_module');
		$select->order("{$orderby} asc");
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchAll($sql);
	}	
	
	public function getCount($departmentName=''){
		$where = $this->_table->getAdapter()->quoteInto("?",1);	
    	if (isset($departmentName) && ''!=$departmentName) {
			$where.=$this->_table->getAdapter()->quoteInto(" And  ura_title=?",$departmentName);
    	}		
		return $this->_table->fetchRow($where);	
	}	

	
	/**
	*Get Rowset By Department Name
	*@param string $ura_id 部门id
	*@return Rowset
	*/
	public function getByName($departmentName){
	
		$select = $this->_table->select()->where('ud_name = ?', $departmentName);	 
		
		return $this->_table->fetchRow($select);
		
	}
	
	/**
	*Get Rowset By Department 
	*@param string $ura_id 
	*@return Rowset
	*/
	public function getById($ura_id){
		$where = $this->_table->getAdapter()->quoteInto("ura_id = ?",$ura_id);	
		return $this->_table->fetchRow($where)->toArray();		
	}
	
	public function getByfield($field=1,$value=1){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);	
		return $this->_table->fetchAll($where)->toArray();		
	}	
	
	/**
	* 根据条件返回一行信息
	*/
	public function getRowByMCA($module,$controller,$action)
	{
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,"*");
        $select->where("1 =?",1);
        $select->where(" ura_module =?",$module);
        $select->where(" ura_controller =?",$controller);
        $select->where(" ura_action =?",$action);
        $sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	/**
	* 返回所有模块名称
	*/
	public function getAllModule(){
		$select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,"ura_module");
        $select->group('ura_module');
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchAll($sql);
	}
		
}